package com.timedomain.him.remake.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.timedomain.him.remake.R;
import com.timedomain.him.remake.ui.MorningCallActivity;

import org.jetbrains.annotations.Nullable;

import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
import uk.co.chrisjenx.calligraphy.CalligraphyUtils;


public class BaseActivity extends Activity {

    private Handler hWnd = new Handler();

    public Runnable setTimeout(Runnable r,int ms){
        hWnd.postDelayed(r,ms);
        return r;
    }

    private boolean timerEnabled = false;
    private int timerInterval = 500;
    public void setTimerEnabled(boolean enabled){
        if(enabled == this.timerEnabled){return;}
        timerEnabled = enabled;
        if(enabled){
            startTimer();
        }
        else{
            stopTimer();
        }
    }

    public void setTimerInterval(int interval){
        if(interval < 1){interval = 1;}
        timerInterval = interval;
        if(isTimerEnabled()){
            stopTimer();
            startTimer();
        }
    }

    public boolean isTimerEnabled(){
        return timerEnabled;
    }

    public int getTimerInterval(){
        return timerInterval;
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(isTimerEnabled()){
            stopTimer();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    private void stopTimer(){
        hWnd.removeCallbacks(this.onTimerInternal);
    }

    private void startTimer(){
        hWnd.removeCallbacks(this.onTimerInternal);
        hWnd.postDelayed(this.onTimerInternal,timerInterval);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isTimerEnabled()){
            startTimer();
        }
    }
    // 不要用方法引用，会无法销毁定时器
    Runnable onTimerInternal = new Runnable() {
        @Override
        public void run() {
            onTimer();
            hWnd.postDelayed(BaseActivity.this.onTimerInternal,timerInterval);
        }
    };


    protected void onTimer(){

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adaptScreen();
    }


    private boolean __hasWindowSeen = false;

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(!__hasWindowSeen && hasFocus){
            onUiPrepared();
            __hasWindowSeen = true;
        }
    }

    protected void onUiPrepared(){}

    private void adaptScreen(){
        View decor = this.getWindow().getDecorView();
        this.getWindow().setNavigationBarColor(Color.TRANSPARENT);
        if (Color.blue(getWindow().getStatusBarColor()) > 127) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR );
                }
                else{

                    decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR );
                }
                View contentView = this.findViewById(android.R.id.content);
                contentView.setPadding(contentView.getPaddingLeft(),contentView.getPaddingTop(),contentView.getPaddingRight(),contentView.getPaddingBottom());
            }
        }
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
            this.getWindow().addFlags(WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT);
        }
    }

    private  Toast __t = null;

    public void showToast(String str){
        if(__t != null){
            try{__t.cancel();}catch (Exception ex){}
        }
        Toast t = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        __t = t;

        View v = getLayoutInflater().inflate(R.layout.toast_frame,null);
        TextView view = v.findViewById(R.id.txtToast);
        v.setMinimumWidth(getWindow().getDecorView().getWidth());

        view.setText(str);
        t.setView(v);
        t.setGravity(Gravity.TOP,0,(int)dp2pix(64));
        t.show();
    }
    private float dp2pix(float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
    }

    public void gotoUrl(String str) {
        try {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(str));
            startActivity(i);
        }catch (Exception ex){
            showToast("无法打开链接："+str);
        }
    }

    public void goback(View view) {
        finish();
    }
}
